package org.example.game.biweekly.game136;

import java.util.Arrays;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/08/03 20:46
 */
public class Solution003 {

    public static void main(String[] args) {
        System.out.println(new Solution003().minFlips2(new int[][]{{1,0,0},{0,1,0},{0,0,1}}));
    }

    public int minFlips2(int[][] grid) {
        //行 列都回文的最优解， 是找出 四个回文数字， 统计 1,0 的数量， 取数量小的
        //计算出要翻转的数量之后， 考虑1的数量
        //如果行、列都是回文了， 数字1 不是4个倍数， 说明 1在 行或者列的中间， 直接翻转 1就行

        int sum = 0;
        int t01 = 0;
        int t11 = 0;
        for (int i = 0; i < grid.length / 2 + 1; i++) {
            for (int j = 0; j < grid[i].length / 2 + 1; j++) {

                // 找到这个点 对应的 4个 或者 2个点 或者 自己
                int xEnd = grid.length - 1 - i;
                int yEnd = grid[i].length - 1 - j;

                if(xEnd < i || yEnd < j){
                    continue;
                }

                if(i == xEnd && j == yEnd){
                    if(grid[i][j] != 0){
                        sum++;
                    }
                } else if (i == xEnd) {
                    //两个点
                    if(grid[i][j] != grid[xEnd][yEnd]){
                        sum++;
                        t01++;
                    }
                    if(grid[i][j] + grid[xEnd][yEnd] == 2){
                        t11++;
                    }
                } else if (j == yEnd) {
                    //两个点
                    if(grid[i][j] != grid[xEnd][yEnd]){
                        sum++;
                        t01++;
                    }
                    if(grid[i][j] + grid[xEnd][yEnd] == 2){
                        t11++;
                    }
                } else {
                    // 4个点， 翻少的点
                    int count0 = 0;
                    int count1 = 0;
                    if(grid[i][j] == 0){
                        count0++;
                    }else {
                        count1++;
                    }

                    if(grid[i][yEnd] == 0){
                        count0++;
                    }else {
                        count1++;
                    }

                    if(grid[xEnd][j] == 0){
                        count0++;
                    }else {
                        count1++;
                    }

                    if(grid[xEnd][yEnd] == 0){
                        count0++;
                    }else {
                        count1++;
                    }
                    sum += Math.min(count0, count1);
                }
            }
        }

        return sum + (t11%2!=0 && t01==0? 2: 0);
    }

    public int minFlips(int[][] grid) {
        int[][] copy = new int[grid.length][];
        for (int i = 0; i < grid.length; i++) {
            copy[i] = Arrays.copyOf(grid[i], grid[i].length);
        }
        int sum0 = minflips0(grid, 0);
        int sum1 = minflips0(copy, 1);
        return Math.min(sum0, sum1);
    }



    private int minflips0(int[][] grid, int value) {
        //行 列都回文的最优解， 是找出 四个回文数字， 统计 1,0 的数量， 取数量小的
        //计算出要翻转的数量之后， 考虑1的数量
        //如果行、列都是回文了， 数字1 不是4个倍数， 说明 1在 行或者列的中间， 直接翻转 1就行

        int sum = 0;
        for (int i = 0; i < grid.length / 2 + 1; i++) {
            for (int j = 0; j < grid[i].length / 2 + 1; j++) {
                // 找到这个点 对应的 4个 或者 2个点 或者 自己
                int xEnd = grid.length - 1 - i;
                int yEnd = grid[i].length - 1 - j;

                if(i == xEnd && j == yEnd){
                    continue;
                } else if (i == xEnd) {
                    //两个点
                    if(grid[i][j] != grid[xEnd][yEnd]){
                        grid[i][j] = value;
                        grid[xEnd][yEnd] = value;
                        sum++;
                    }
                } else if (j == yEnd) {
                    //两个点
                    if(grid[i][j] != grid[xEnd][yEnd]){
                        grid[i][j] = value;
                        grid[xEnd][yEnd] = value;
                        sum++;
                    }
                } else {
                    // 4个点， 翻少的点
                    int count0 = 0;
                    int count1 = 0;
                    if(grid[i][j] == 0){
                        count0++;
                    }else {
                        count1++;
                    }

                    if(grid[i][yEnd] == 0){
                        count0++;
                    }else {
                        count1++;
                    }

                    if(grid[xEnd][j] == 0){
                        count0++;
                    }else {
                        count1++;
                    }

                    if(grid[xEnd][yEnd] == 0){
                        count0++;
                    }else {
                        count1++;
                    }
                    if(count0 >= count1){
                        grid[i][j] = 0;
                        grid[xEnd][j] = 0;
                        grid[i][yEnd] = 0;
                        grid[xEnd][yEnd] = 0;
                    }else {
                        grid[i][j] = 1;
                        grid[xEnd][j] = 1;
                        grid[i][yEnd] = 1;
                        grid[xEnd][yEnd] = 1;
                    }
                    sum += Math.min(count0, count1);
                }
            }
        }

        int sum1 = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if(grid[i][j] == 1){
                    sum1++;
                }
            }
        }

        return sum + sum1%4;
    }

}
